<?php
$v_record_id = 0;
if(isset($_REQUEST['hdn_record_id'])){
	$v_record_id = intval($_REQUEST['hdn_record_id']);
}
//Doan nay can them vao de thuc hien cap nhat theo bo DEV-EDITOR
SetDevEditPath($DEVEDIT_PATH);
$short_des = new DevEdit();
$short_des->SetName("txt_detail");
$v_content = _replace_bad_char($short_des->GetValue(false)); //Thuc hien de cap nhat noi dung
////////////
$v_record_doc_id = $_REQUEST['hdn_record_doc_id'];
$v_name = _replace_bad_char($_REQUEST['txt_name']);
$v_code = _replace_bad_char($_REQUEST['txt_code']); 
$v_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['txt_date']);
$v_promulgate = _replace_bad_char($_REQUEST['txt_promulgate']); 
$v_deleted_exist_file_id_list = $_REQUEST['hdn_deleted_exist_file_id_list']; 
 //$v_file_name = $_REQUEST['file_attach'];
$v_url = "index.php?modal_dialog_mode=1" . "&hdn_record_doc_id=" . $v_record_doc_id . "&fuseaction=DISPLAY_SINGLE_RECORD_DOC" ;
//Kiem tra quyen cua NSD
if (0>1){
	$function_code = _CONST_ONEGATE_AF_UPDATE;
	if(!_check_permission_on_function($_SESSION['staff_id'], $_ISA_APP_CODE, $function_code)){?>
		<script>
			alert("<? echo _CONST_FUNCTION_PERMISSION_ERROR; ?>");
			if (_MODAL_DIALOG_MODE==1){
				window.location = "<? echo $v_url; ?>";
			}else{
				window.history.back();
			}	
		</script>
		<?php
	}
}
$v_new_file_id_list = "";
$v_form_field = 'file_attach';

if (isset($_FILES[$v_form_field]['tmp_name'])){
		$v_record_doc_ = $v_record_doc_id;
		if($v_record_doc_id==0||$v_record_doc_id==''||empty($v_record_doc_id)){
		if(_is_sqlserver()){
			$sql = "SELECT MAX(PK_RECORD_DOC) AS NEW_RECORD_DOC FROM dbo.T_ONEGATE_RECORD_DOC";
			$result = mssql_query($sql,$conn);
			$rs = mssql_fetch_array($result);
			$v_record_doc_ = $rs['NEW_RECORD_DOC']+1;
		}
                }
		$v_filename = $v_record_id.'_'.$v_record_doc_.'_doc_'._replace_bad_char(trim($_FILES[$v_form_field]['name']));
		$v_tmp_filename = trim($_FILES[$v_form_field]['tmp_name']);
		$v_file_id = 0;
		if ($v_tmp_filename!="" and !is_null($v_tmp_filename)){
			_copy_file($v_tmp_filename, _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
			if ($v_new_file_id_list ==""){
				$v_new_file_id_list = _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
			}else{			
				$v_new_file_id_list = $v_new_file_id_list . ",". _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
			}	
		}	
}	


////////////////////////////////
$v_fuseaction_back = "DISPLAY_ALL_RECORD_DOC";

if(_is_sqlserver()){

	$v_text_id = _save_text_to_database('T_TEMP_FILE', 'C_TEXT', $v_content);
	
	$sql = "Exec Onegate_RecordDocUpdate " ;
	$sql = $sql . $v_record_doc_id;
	$sql = $sql . "," .$v_record_id;
	$sql = $sql . ",'" . $v_name . "'";
	$sql = $sql . ",'" . $v_code . "'";
	$sql = $sql . ",'" .$v_date . "'";
	$sql = $sql . ",'" . $v_promulgate . "'";
	$sql = $sql . "," .$v_text_id;
	//$sql = $sql . ",'" . $v_filename. "'";
	$sql = $sql . ",'" . $v_new_file_id_list. "'";
	$sql = $sql . ",'" . $v_deleted_exist_file_id_list . "'";
	//echo $sql; exit;
	$result = mssql_query($sql,$conn);
	$rs = mssql_fetch_array($result);
	$v_record_doc_id = $rs['NEW_ID'];
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	@mssql_free_result($result);
}

?>
<form action="index.php" method="post" name="f_back">
	<input type="hidden" name="fuseaction" value="<?php echo $v_fuseaction_back; ?>">
	<input type="hidden" name="hdn_record_id"  value="<?php echo $v_record_id; ?>">
	<input name="hdn_record_doc_id" type="hidden" value="<?php echo $v_record_doc_id; ?>">
	<input name="hdn_code" type="hidden" value="<?php echo $v_code; ?>">
	<input name="hdn_name" type="hidden" value="<?php echo $v_name; ?>">
	<input name="hdn_promulgate" type="hidden" value="<?php echo $v_promulgate; ?>">
	<input name="hdn_fisrt" type="hidden" value="<?php echo $v_fisrt; ?>">
</form>
<Script language="javascript">
	document.f_back.submit();	
</Script>
